package practice;

import java.io.*;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * Author:  LiuTao
 * Version: 1.0
 * Date:    2024-04-02  10:49
 */
public class Demo3 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入查找的路径");
        String rootPath = scanner.next();

        File rootFile = new File(rootPath);
        if(!rootFile.isDirectory()) {
            System.out.println("您输入的路径有误");
            return;
        }

        System.out.println("输入您要查询的关键字");
        String word = scanner.next();


    }

    private static void scanDir(File rootFile, String word) throws FileNotFoundException {
        File[] files = rootFile.listFiles();
        if(files == null) {
            return;
        }
        for(File f : files) {
            if(f.isDirectory()) {
                scanDir(f,word);
            } else if (f.isFile()) {
                searchInFile(f,word);
            }
        }
    }

    private static void searchInFile(File f, String word) throws FileNotFoundException {
        try(InputStream inputStream = new FileInputStream(f)) {
            StringBuffer stringBuffer = new StringBuffer();
            while(true) {
                byte[] buffer = new byte[1024];
                int n = inputStream.read(buffer);
                if(n == -1) {
                    break;
                }
                String str = new String(buffer,0,n);
                stringBuffer.append(str);
            }
            if(stringBuffer.indexOf(word) == -1) {
                System.out.println("没找到");
                return;
            }
            System.out.println("找到" + word + "路径" + f.getAbsolutePath() );
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
